package com.pdstudio.actualtime;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MySurfaceView extends SurfaceView implements
		SurfaceHolder.Callback, Runnable {

	private SurfaceHolder mSurfaceHolder = null;
	private int count = 0;
	private Context cot;

	public MySurfaceView(Context context) {
		super(context);
		cot = context;
		mSurfaceHolder = this.getHolder();
		mSurfaceHolder.addCallback(this);
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	public void surfaceCreated(SurfaceHolder holder) {
		new Thread(this).start();
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
	}

	public void run() {
		while (true) {
			try {
				Thread.sleep(3000);
			} catch (Exception e) {

			}
			synchronized (mSurfaceHolder) {
				// Draw();
				RemoteDraw();
			}
		}
	}

	public void Draw() {
		Canvas canvas = mSurfaceHolder.lockCanvas();
		if (mSurfaceHolder == null || canvas == null) {
			return;
		}
		InputStream iso;
		Bitmap bitmap = null;
		try {
			iso = cot.getAssets().open("pics/" + count + ".png");
			bitmap = BitmapFactory.decodeStream(iso);
		} catch (IOException e) {
			e.printStackTrace();
		}
		canvas.drawRGB(255, 255, 255);
		canvas.drawBitmap(bitmap, 300 / 2 - bitmap.getWidth() / 2,
				300 / 2 - bitmap.getHeight() / 2, null);
		mSurfaceHolder.unlockCanvasAndPost(canvas);
		count++;
		if (count > 11) {
			count = 0;
		}
	}

	private InputStream GetRemoteStream(String path) {
		InputStream instream = null;
		try {
			URL url = new URL(path);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setConnectTimeout(5000);
			conn.setRequestMethod("GET");
			if (conn.getResponseCode() == 200) {
				instream = conn.getInputStream();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return instream;
	}

	// 定义一个根据图片url获取InputStream的方法
	private byte[] getBytes(InputStream is) throws IOException {
		ByteArrayOutputStream outstream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024]; // 用数据装
		int len = -1;
		while ((len = is.read(buffer)) != -1) {
			outstream.write(buffer, 0, len);
		}
		outstream.close();
		// 关闭流一定要记得。
		return outstream.toByteArray();
	}

	// 显示远程的图片，根据draw改写
	public void RemoteDraw() {
		Canvas canvas = mSurfaceHolder.lockCanvas();
		if (mSurfaceHolder == null || canvas == null) {
			return;
		}
		InputStream iso;
		Bitmap bitmap = null;
		try {
			// iso = cot.getAssets().open("pics/" + count + ".png");
			// iso=GetRemoteStream("http://192.168.0.19:8011/"+count+".png");
			iso = GetRemoteStream("http://113.247.234.237//Services/NewsAttachment.ashx?AttachmentID="
					+ count);
			// 然后使用方法decodeByteArray（）方法解析编码，生成Bitmap对象。
			byte[] data = getBytes(iso);
			Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
			bitmap = bm; // BitmapFactory.decodeStream(iso);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (bitmap != null) {
			canvas.drawRGB(255, 255, 255);
			canvas.drawBitmap(bitmap, 300 / 2 - bitmap.getWidth() / 2,
					300 / 2 - bitmap.getHeight() / 2, null);
		}
		mSurfaceHolder.unlockCanvasAndPost(canvas);
		count++;
		if (count > 11) {
			count = 0;
		}
	}
}